/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.administration;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import database.StaffAuthenticationDAO;
import java.util.Map;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;

/**
 *
 * @author Thai Ha Cong
 */
@Namespace("/Administration")
public class LoginAction extends ActionSupport {

    private String loginName;
    private String password;

    public LoginAction() {
    }

    public String getLoginName() {
        return loginName;
    }

    @Validations(
            requiredStrings = {
        @RequiredStringValidator(type = ValidatorType.FIELD, message = "Login Name is required.")},
            stringLengthFields = {
        @StringLengthFieldValidator(type = ValidatorType.FIELD, message = "Login Name should be from 6 to 30 chracters.", minLength = "6", maxLength = "30")})
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    @Validations(
            requiredStrings = {
        @RequiredStringValidator(type = ValidatorType.FIELD, message = "Password is required.")},
            stringLengthFields = {
        @StringLengthFieldValidator(type = ValidatorType.FIELD, message = "Password should be from 6 to 30 chracters.", minLength = "6", maxLength = "30")})
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    @Action(value = "login", results = {
        @Result(name = "success", location = "/redirect/ardrHome.jsp"),
//        @Result(name = "success", location = "/administration/shome.jsp"),
        @Result(name = "input", location = "/administration/slogin.jsp")
    })
    public String execute() throws Exception {
        StaffAuthenticationDAO staffAuthenticationDAO = new StaffAuthenticationDAO();

        Staff staff = new Staff();
        staff.setLoginName(loginName);
        staff.setPassword(password);

        if (staffAuthenticationDAO.checkLogin(staff)) {
            staff = staffAuthenticationDAO.getStaffFromLoginName(loginName);

            if (staffAuthenticationDAO.addAction("login", staff.getIdStaff())) {
                Map session = ActionContext.getContext().getSession();
                session.put("staff", staff);

                addActionMessage("You logged in with these following information:<br>" + staff.toString());

                return SUCCESS;
            } else {
                addActionError("There may be some errors. Try again later.");

                return INPUT;
            }
        } else {
            addActionError("Login Failed.");

            return INPUT;
        }
    }

    @Action(value = "logout", results = {
        @Result(name = "success", location = "/redirect/ardrHome.jsp")
    })
    @SkipValidation
    public String logout() throws Exception {
        StaffAuthenticationDAO staffAuthenticationDAO = new StaffAuthenticationDAO();
        
        Map session = ActionContext.getContext().getSession();
        Staff staff = (Staff) session.get("staff");
        
        staffAuthenticationDAO.addAction("logout", staff.getIdStaff());
        session.remove("staff");

        addActionMessage("Logged out successfully.");
        return SUCCESS;
    }
}